package com.wanmait.movies.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.movies.dao.ActorDao;
import com.wanmait.movies.dao.ClickDao;
import com.wanmait.movies.dao.CollectDao;
import com.wanmait.movies.dao.DirectorDao;
import com.wanmait.movies.dao.DiscussDao;
import com.wanmait.movies.dao.GiveGradesDao;
import com.wanmait.movies.dao.MovieActorDao;
import com.wanmait.movies.dao.MovieDao;
import com.wanmait.movies.dao.MovieDirectorDao;
import com.wanmait.movies.dao.ReportDao;
import com.wanmait.movies.vo.Actor;
import com.wanmait.movies.vo.Director;
import com.wanmait.movies.vo.Discuss;
import com.wanmait.movies.vo.Movie;
import com.wanmait.movies.vo.MovieType;
import com.wanmait.movies.vo.Pager;
import com.wanmait.movies.vo.User;

/**
 * Servlet implementation class MoviesServlet
 */
@WebServlet("/MoviesServlet")
public class MoviesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MoviesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String action = request.getParameter("action");
		if("showovies".equals(action)) {
			this.showovies(request, response);
		} 
		if("timetype".equals(action)) {
			this.timetype(request, response);//�������Ͳ��ҵ�Ӱ
		}  
		if("huanye".equals(action)) {//ÿ�˸�һҳ�������ҳ
			this.huanye(request, response);
		}  
		if("Search".equals(action)) {//����ģ������
			this.Search(request, response);
		}  
		if("Searchfenye".equals(action)) {
			this.Searchfenye(request, response);
		}
		if("dafen".equals(action)) {
			this.dafen(request, response);
		}
		if("dianzan".equals(action)) {
			this.dianzan(request, response);
		}
		if("quxiaodianzan".equals(action)) {
			this.quxiaodianzan(request, response);
		}
		if("shoucang".equals(action)) {
			this.shoucang(request, response);
		}
		if("quxiaoshoucang".equals(action)) {
			this.quxiaoshoucang(request, response);
		}
		if("pinglun".equals(action)) {
			this.pinglun(request, response);
		}
		if("pingluntaren".equals(action)) {
			this.pingluntaren(request, response);
		}
		if("jubao".equals(action)) {
			this.jubao(request, response);
		} 
		if("getSearchMovies".equals(action)) {
			getSearchMovies(request, response);
		}
		if("findMovieByDirector".equals(action)){
			this.findMovieByDirector(request, response);
		}
		if("findMovieByActor".equals(action)){
			this.findMovieByActor(request, response);
		}
		if("previouPage".equals(action)) {
			this.getAllDiscusssAjax(request, response);
		}
		if("nextPage".equals(action)) {
			this.getAllDiscusssAjaxNextPage(request, response);
		}
	}
	protected void getSearchMovies(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String searchword = request.getParameter("searchword");
		ArrayList<Movie> movies = MovieDao.getMovieDao().getSearchMovieList(searchword);
	}
	protected void Searchfenye(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	      String fenye=request.getParameter("dianjifenye");
	      String neirong=request.getParameter("neirong");
	      int row= MovieDao.getMovieDao().alllikecount(neirong);
	      Movie movie=new Movie();
		    movie.setPageNo(Integer.parseInt(fenye));
		    movie.setPagerow(row);
		    movie.setPageSize(8);
		   ArrayList<Movie>likemovie= MovieDao.getMovieDao().alllikemovie(movie, neirong);
		    request.setAttribute("likemovie",likemovie);
			request.setAttribute("row", row);
			request.getRequestDispatcher("").forward(request, response);
	}
	
	protected void  Search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    String neirong=request.getParameter("neirong");
	    int row= MovieDao.getMovieDao().alllikecount(neirong);
	    Movie movie=new Movie();
	    movie.setPageNo(row);
	    movie.setPagerow(1);
	    movie.setPageSize(8);
	   ArrayList<Movie>likemovie= MovieDao.getMovieDao().alllikemovie(movie, neirong);
	    request.setAttribute("likemovie",likemovie);
		request.setAttribute("row", row);
		request.getRequestDispatcher("").forward(request, response);
	}
	protected void huanye(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sum=request.getParameter("huanye");//�����ҳ��
        String timestamp=request.getParameter("timestamp");//����ʱ�任ҳ
        String typeid=request.getParameter("typeid");//�������ͻ�ҳ
        int typeids=Integer.parseInt(typeid);
    	if(timestamp!=null&&typeid==null) {//ֻ�ṩ���޲��ҵ�Ӱ�ӵ�һ�����ڰ˲�
    		int row=MovieDao.getMovieDao().getTimeMovie(timestamp);
    		MovieType movietype=new MovieType();
    		movietype.setPagerow(row);//������
    		movietype.setPageSize(8);//���²鼸��
    		movietype.setPageNo(Integer.parseInt(sum));//�ӵڼ�����ʼ��
    		ArrayList<Movie>timemovie=MovieDao.getMovieDao().gettimemovie(timestamp, movietype);
    		request.setAttribute("timemovie",timemovie );
    		request.setAttribute("row", row);
    		request.getRequestDispatcher("").forward(request, response);
    	}else if(timestamp==null&&typeid!=null) {
    		int row=MovieDao.getMovieDao().getCountType(typeids);
    		MovieType movietype=new MovieType();
    		movietype.setPagerow(row);
    		movietype.setPageSize(8);
    		movietype.setPageNo(Integer.parseInt(sum));
    		ArrayList<Movie>typemovie=MovieDao.getMovieDao().getTypeMovie(movietype);
    		request.setAttribute("row", row);
    		request.setAttribute("typemovie", typemovie);
    		request.getRequestDispatcher("").forward(request, response);
    	}else {
    		int row=MovieDao.getMovieDao().gettimetypecount(timestamp, typeids);
    		MovieType movietype=new MovieType();
    		movietype.setPagerow(row);
    		movietype.setPageSize(8);
    		movietype.setPageNo(Integer.parseInt(sum));
    		ArrayList<Movie>typetime=MovieDao.getMovieDao().alltypetime(movietype, timestamp);
    		request.setAttribute("row", row);
    		request.setAttribute("typetime", typetime);
    		request.getRequestDispatcher("").forward(request, response);
    	}
	}
	protected void timetype(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    String timestamp=request.getParameter("timestamp");//�������Ͳ��ҵ�Ӱ
	    String typeid=request.getParameter("typeid");
	    int typeids=Integer.parseInt(typeid);
		if(timestamp!=null&&typeid==null) {//ֻ�ṩ���޲��ҵ�Ӱ�ӵ�һ�����ڰ˲�
			int row=MovieDao.getMovieDao().getTimeMovie(timestamp);
			MovieType movietype=new MovieType();
			movietype.setPagerow(row);//������
			movietype.setPageSize(8);//���²鼸��
			movietype.setPageNo(1);//�ӵڼ�����ʼ��
			ArrayList<Movie>timemovie=MovieDao.getMovieDao().gettimemovie(timestamp, movietype);
			request.setAttribute("timemovie",timemovie );
			request.setAttribute("row", row);
			request.getRequestDispatcher("").forward(request, response);
		}else if(timestamp==null&&typeid!=null) {
			int row=MovieDao.getMovieDao().getCountType(typeids);
			MovieType movietype=new MovieType();
			movietype.setPagerow(row);
			movietype.setPageSize(8);
			movietype.setPageNo(1);
			ArrayList<Movie>typemovie=MovieDao.getMovieDao().getTypeMovie(movietype);
			request.setAttribute("row", row);
			request.setAttribute("typemovie", typemovie);
			request.getRequestDispatcher("").forward(request, response);
		}else {
			int row=MovieDao.getMovieDao().gettimetypecount(timestamp, typeids);
			MovieType movietype=new MovieType();
			movietype.setPagerow(row);
			movietype.setPageSize(8);
			movietype.setPageNo(1);
			ArrayList<Movie>typetime=MovieDao.getMovieDao().alltypetime(movietype, timestamp);
			request.setAttribute("row", row);
			request.setAttribute("typetime", typetime);
			request.getRequestDispatcher("").forward(request, response);
		}
	 
	
	}
	//点击导演搜索电影
	protected void findMovieByDirector(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		Director director = DirectorDao.getDao().findById(Integer.parseInt(id));
		request.setAttribute("director", director);
		
		ArrayList<Movie> lists = MovieDao.getMovieDao().findMoviesByDirectorId(Integer.parseInt(id));
		request.setAttribute("lists", lists);
		request.getRequestDispatcher("/ActorDirecTorMovie.jsp").forward(request, response);
		
	}
	
	//点击演员搜索电影 
	protected void findMovieByActor(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		Actor actor = ActorDao.getDao().findById(Integer.parseInt(id));
		request.setAttribute("actor", actor);
		
		
	
		ArrayList<Movie> lists = MovieDao.getMovieDao().findMoviesByActorId(Integer.parseInt(id));
		//System.out.println(lists.size());
		request.setAttribute("lists", lists);
		request.getRequestDispatcher("/ActorDirecTorMovie.jsp").forward(request, response);
		
	}
	
	

	//��ʾ��Ӱҳ�棬��Ӱ���
	protected void showovies(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		User ui = (User)request.getSession().getAttribute("ui");
		 
		int movieId =Integer.parseInt(request.getParameter("movieId"));
		 
		Movie movie = MovieDao.getMovieDao().movieGetMovieId(movieId);
		request.setAttribute("movie", movie);
		ArrayList<Actor> allActor=MovieActorDao.getMovieActorDao().movieActorGetMovieId(movieId);
		request.setAttribute("allActor", allActor);
		double fenshu = GiveGradesDao.getObj().getfenshu( movieId);
		request.setAttribute("fenshu",fenshu);	
		ArrayList<Director> allDirector = MovieDirectorDao.getMovieDirectorDao().movieDirectorGetMovieId(movieId);
		request.setAttribute("allDirector", allDirector);
		
		int rows = DiscussDao.getObj().getRows();
		Pager pager = new Pager();
		pager.setCountinPager(10);//设置每页显示3条记录
		pager.setPagerNum((rows+9)/10);
		pager.setCurrentPager(1);
		request.setAttribute("pager", pager);
		//查询数据
		ArrayList<Discuss> allDiscuss = DiscussDao.getObj().getallDiscuss(movieId, pager);
		
		request.setAttribute("allDiscuss", allDiscuss);
		//将数据保存到request
		request.getRequestDispatcher("/move.jsp").forward(request, response);
		}
	//利用AJax访问上一页
		protected void getAllDiscusssAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String num = request.getParameter("page");//点击的数字
			//查询数据库表中间数据的总记录数  行数 条数
			int rows = DiscussDao.getObj().getRows();
			Pager pager = new Pager();
			pager.setCountinPager(10);//设置每页显示3条记录
			pager.setPagerNum((rows+9)/10);
			pager.setCurrentPager(Integer.parseInt(num));
			request.setAttribute("pager", pager);
			String a = request.getParameter("movieId");
			int movieId=Integer.parseInt(a);
			//查询数据
			ArrayList<Discuss> allDiscuss = DiscussDao.getObj().getallDiscuss(movieId, pager);
			request.setAttribute("allDiscuss", allDiscuss);
			//将数据保存到request
			//-->Ajax的mes
			request.getRequestDispatcher("/pinglun.jsp").forward(request, response);
		}
		
		//利用AJax访问下一页
		protected void getAllDiscusssAjaxNextPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String num = request.getParameter("page");//点击的数字
			//查询数据库表中间数据的总记录数  行数 条数
			int rows = DiscussDao.getObj().getRows();
			Pager pager = new Pager();
			pager.setCountinPager(10);//设置每页显示3条记录
			pager.setPagerNum((rows+9)/10);
			pager.setCurrentPager(Integer.parseInt(num));
			request.setAttribute("pager", pager);
			String a = request.getParameter("movieId");
			int movieId=Integer.parseInt(a);
			//查询数据
			ArrayList<Discuss> allDiscuss = DiscussDao.getObj().getallDiscuss(movieId, pager);
			request.setAttribute("allDiscuss", allDiscuss);
			//将数据保存到request
			//-->Ajax的mes
			request.getRequestDispatcher("/pinglun.jsp").forward(request, response);
		}
		
		

	
	protected void dafen(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int fenshu = Integer.parseInt(request.getParameter("fenshu"));
		int movieId = Integer.parseInt(request.getParameter("movieId"));
		//int userId=user.getId();
		//�жϸ��û��Ըõ�Ӱ�Ƿ�����
		User user = (User)request.getSession().getAttribute("ui");
		//int userId=user.getId();
		//�жϸ��û��Ըõ�Ӱ�Ƿ�����
		if(user==null) {
			response.getWriter().print("0");
		}
		else {
		int userId=user.getId();
		boolean flag = GiveGradesDao.getObj().ifDaFen(userId, movieId);
		if(!flag) {
			GiveGradesDao.getObj().dafen(userId, fenshu, movieId);
		}
		else {
			GiveGradesDao.getObj().xiugaidafen(userId, fenshu, movieId);
		}
		double pingjunfen = GiveGradesDao.getObj().getfenshu( movieId);
		response.getWriter().print(pingjunfen);
		}
	}
	
	
	protected void dianzan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int movieId =Integer.parseInt(request.getParameter("movieId"));
		User user = (User)request.getSession().getAttribute("ui");
		//System.out.println(user);
		if(user==null) {
			response.getWriter().print("0");
		}
		else {
		int userId=user.getId();
		ClickDao.getObj().dainzan(userId, movieId);
		}
	}
	
	//���û��ĵ���ɾ��
	protected void quxiaodianzan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int movieId =Integer.parseInt(request.getParameter("movieId"));
		
		User user = (User)request.getSession().getAttribute("ui");
		if(user==null) {
			response.getWriter().print("0");
		}
		else {
		int userId=user.getId();
		ClickDao.getObj().quxiaodianzan(userId, movieId);
		}
	}
	
	
	//���û����ղ���ӵ����ݿ�
	protected void shoucang(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int movieId =Integer.parseInt(request.getParameter("movieId"));
		User user = (User)request.getSession().getAttribute("ui");
		if(user==null) {
			response.getWriter().print("0");
		}
		else {
		int userId=user.getId();
		CollectDao.getObj().shoucang(userId, movieId);
		}
	}
	
	
	
	//���û����ղ�ɾ��
	protected void quxiaoshoucang(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int movieId =Integer.parseInt(request.getParameter("movieId"));
		User user = (User)request.getSession().getAttribute("ui");
		
		if(user==null) {
			response.getWriter().print("0");
		}
		else {
		int userId=user.getId();
		CollectDao.getObj().quxiaoshoucang(userId, movieId);
		}
	}
	
	//���û��Ե�Ӱ��������ӵ����ݿ�
	protected void pinglun(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String discuss = request.getParameter("pinglun");
		String movieId = request.getParameter("movieId");
		User user = (User)request.getSession().getAttribute("ui");
		if(user==null) {
			response.sendRedirect("/movies/denglupage.jsp");
		}
		else {
		int userId=user.getId();
		DiscussDao.getObj().pinglun(userId, discuss,Integer.parseInt(movieId));
		String str=request.getHeader("referer");
		response.sendRedirect(str);
		}
	}
	
	//���û��������û���������ӵ����ݿ�
	protected void pingluntaren(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String discuss = request.getParameter("pingluntarentext");
		String reId = request.getParameter("reId");
		String movieId = request.getParameter("movieId");
		User user = (User)request.getSession().getAttribute("ui");
		if(user==null) {
			response.sendRedirect("/movies/denglupage.jsp");
		}
		else {
		int userId=user.getId();
		DiscussDao.getObj().pingluntaren(userId, discuss,Integer.parseInt(reId),Integer.parseInt(movieId));
		String str=request.getHeader("referer");
		response.sendRedirect(str);
		}
	}
	
	//���û��ľٱ���ӵ����ݿ�
	protected void jubao(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user = (User)request.getSession().getAttribute("ui");
		
		if(user==null) {
			response.getWriter().print("0");
		}
		else {
		int userId=user.getId();
		String pinglunId = request.getParameter("pinglunId");
		ReportDao.getObj().getjubao(userId, Integer.parseInt(pinglunId));
		}
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
